package com.example.demo.servlet;

import org.springframework.boot.web.server.Http2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author Rocket
 * @version 10.0.1
 * @date 2021-08-15 10:12 AM
 */
@WebServlet(name="byte servlet", urlPatterns = {"/byte"})
public class ByteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain;charset=utf-8");

        String hello = "你好";
        resp.setStatus(200);

        URL url = this.getClass().getClassLoader().getResource("static/wechat.jpg");
        String fileName = url.getFile();

        FileInputStream fis = new FileInputStream(fileName);
        byte[] content = new byte[8092];
        OutputStream os = resp.getOutputStream();
        int count = 0;
        while((count = fis.read(content)) != -1) {
            System.out.println("count = " + count);
            os.write(content, 0, count);
        }


        os.flush();
        resp.setHeader("Cache-Control", "max-age=1800");
        resp.setHeader("Expires", "" + System.currentTimeMillis()+60*1000);
        //resp.setHeader("Pragma", "no-cache");

    }
}
